package net.suncaper.demo.controller;

import net.suncaper.demo.domain.*;
import net.suncaper.demo.service.GraduateService;
import net.suncaper.demo.service.QuestionService;
import net.suncaper.demo.service.ReplyService;
import net.suncaper.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/reply")
public class ReplyController {
    private int select;
    private String UserId;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserService userService;

    @RequestMapping("/arrangement")
    public String Arrangement(Model model,Model mode0) {
        List<Reply> replies=replyService.findReply();
        List<Reply> replies2=new ArrayList<Reply>();
        List<Reply> replies3=new ArrayList<Reply>();
        List<Arrangement> arrangements=replyService.findArrangement();
        int a=0;
        for(int i=0;i<replies.size();i++){
            for(int j=0;j<arrangements.size();j++){
                 if(arrangements.get(j).getReplyId().equals(replies.get(i).getReplyId())){
                     a=1;
                 }
            }
            if(a==0){
                replies2.add(replies.get(i));
            }
            a=0;
        }
        for(int i=0;i<replies2.size();i++){
            if(replies2.get(i).getIsEnd().equals(0)){
                replies3.add(replies2.get(i));
            }
        }
        model.addAttribute("reply", replies3);
        mode0.addAttribute("UserId",UserId);
            return "pages/reply/arrangement.html";
    }
    @RequestMapping("/endarrangement")
    public String EndArrangement(Model model,Model mode0) {
        List<Reply> replies=replyService.findReply();
        List<Reply> replies2=new ArrayList<Reply>();
        List<Reply> replies3=new ArrayList<Reply>();
        List<Arrangement> arrangements=replyService.findArrangement();
        int a=0;
        for(int i=0;i<replies.size();i++){
            for(int j=0;j<arrangements.size();j++){
                if(arrangements.get(j).getReplyId().equals(replies.get(i).getReplyId())){
                    a=1;
                }
            }
            if(a==0){
                replies2.add(replies.get(i));
            }
            a=0;
        }
        for(int i=0;i<replies2.size();i++){
            if(replies2.get(i).getIsEnd().equals(1)){
                replies3.add(replies2.get(i));
            }
        }
        model.addAttribute("reply", replies3);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/arrangement.html";
    }
    @RequestMapping("/havearrangement")
    public String HaveArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(arrangements.get(i).getIsEnd().equals(0)){
                arrangements1.add(arrangements.get(i));
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/haveArrangement.html";
    }
    @RequestMapping("/endhavearrangement")
    public String endHaveArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(arrangements.get(i).getIsEnd().equals(1)){
                arrangements1.add(arrangements.get(i));
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/haveArrangement.html";
    }
    @RequestMapping("/deletearrangement")
    public String deletearrangement(@RequestParam(value = "name", required = false) BigDecimal ArrangementId) {
        replyService.deleteArrangementById(ArrangementId);
        return "redirect:/reply/havearrangement";
    }
    @RequestMapping("/updatearrangement")
    public String updatearrangement(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal ArrangementId) {

        Arrangement  arrangement=replyService.findArrangementId(ArrangementId);
        model.addAttribute("arrangement",arrangement);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/updateArrangement.html";
    }
    @RequestMapping("/saveupdatearrangement")
    public String saveupdatearrangement(Arrangement arrangement,Model model,@RequestParam(value = "name", required = false) BigDecimal ArrangementId) {
        Arrangement arrangement1=replyService.findArrangementId(ArrangementId);
        arrangement.setTeacherId(arrangement1.getTeacherId());
        arrangement.setArrangementId(ArrangementId);
        arrangement.setTeacherName(arrangement1.getTeacherName());
        arrangement.setStudentName(arrangement1.getStudentName());
        arrangement.setReplyId(arrangement1.getReplyId());
        arrangement.setTeacherId(arrangement1.getTeacherId());
        arrangement.setIsEnd(arrangement1.getIsEnd());
        replyService.updateArrangement(arrangement);
        return "redirect:/reply/havearrangement";
    }

    @RequestMapping("/teacherarrangement")
    public String teacherArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(arrangements.get(i).getTeacherId().equals(UserId)&&arrangements.get(i).getIsEnd().equals(0)){
                arrangements1.add(arrangements.get(i));
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/teacherArrangement.html";
    }
    @RequestMapping("/studentarrangement")
    public String studenArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(questionService.findSelectQuestionById(replyService.findReplyById(arrangements.get(i).getReplyId()).getSelectquestionId()).getStudentId().equals(UserId) && arrangements.get(i).getIsEnd().equals(0)){
                arrangements1.add(arrangements.get(i));
                break;
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/StudentArrangement.html";
    }
    @RequestMapping("/endteacherarrangement")
    public String endteacherArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(arrangements.get(i).getTeacherId().equals(UserId)&&arrangements.get(i).getIsEnd().equals(1)){
                arrangements1.add(arrangements.get(i));
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/teacherArrangement.html";
    }
    @RequestMapping("/endstudentarrangement")
    public String endstudenArrangement(Model model,Model mode0) {
        List<Arrangement> arrangements=replyService.findArrangement();
        List<Arrangement> arrangements1=new ArrayList<Arrangement>();
        for(int i=0;i<arrangements.size();i++){
            if(questionService.findSelectQuestionById(replyService.findReplyById(arrangements.get(i).getReplyId()).getSelectquestionId()).getStudentId().equals(UserId) && arrangements.get(i).getIsEnd().equals(1)){
                arrangements1.add(arrangements.get(i));
                break;
            }
        }
        model.addAttribute("arrangement",arrangements1);
        mode0.addAttribute("UserId",UserId);
        return "pages/reply/StudentArrangement.html";
    }
    @RequestMapping("/writearrangement")
    public String WriteArrangement(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal ReplyId) {
        model.addAttribute("replyId",ReplyId);
        mode0.addAttribute("UserId",UserId);
       return  "pages/reply/writeArrangement.html";
    }
//    @RequestMapping("/endwritearrangement")
//    public String endWriteArrangement(Model model,Model mode0,@RequestParam(value = "name", required = false) BigDecimal ReplyId) {
//        model.addAttribute("replyId",ReplyId);
//        mode0.addAttribute("UserId",UserId);
//        return  "pages/reply/endwriteArrangement.html";
//    }
    @PostMapping("/savearrangement")
    public String SaveArrangement(Arrangement arrangement,@RequestParam(value = "replyID", required = false) BigDecimal ReplyId) {
        SelectQuestion selectQuestion=questionService.findSelectQuestionById(replyService.findReplyById(ReplyId).getSelectquestionId());
        GiveQuestion giveQuestion=questionService.findGiveQuestionById(selectQuestion.getQuestionId());
        Teacher teacher=userService.findTeacherById(giveQuestion.getTeacherId());
         arrangement.setReplyId(ReplyId);
         arrangement.setStudentName(replyService.findReplyById(ReplyId).getStudentName());
         arrangement.setQuestionTitle(replyService.findReplyById(ReplyId).getSelectquestionTitle());
         arrangement.setTeacherName(teacher.getName());
         arrangement.setTeacherId(teacher.getTeacherId());
         arrangement.setIsEnd(replyService.findReplyById(ReplyId).getIsEnd());
         replyService.saveArrangement(arrangement);
        return "redirect:/reply/arrangement";
    }

    @RequestMapping("/set")
    public String SetAdministratorId(@RequestParam(value = "UserId", required = false) String UserId,@RequestParam(value = "select", required = false) int select) {
        this.UserId=UserId;
        this.select=select;
        if(select==1){
            return "redirect:/usermanager/set?UserId="+UserId+"&select=1";}
        if(select==2){
            return "redirect:/usermanager/set?UserId="+UserId+"&select=2";}
        else{
            return "redirect:/usermanager/set?UserId="+UserId+"&select=3";}
    }
    @RequestMapping("/saveendreply")
    public String SaveendApply(Reply reply,@RequestParam(value = "name", required = false) BigDecimal questionId){
        List<SelectQuestion> findSelectQuestion=questionService.findSelectQuestion();
        for(int i=0;i<findSelectQuestion.size();i++){
            if(findSelectQuestion.get(i).getQuestionId().equals(questionId)){
                reply.setSelectquestionId(findSelectQuestion.get(i).getSelectQuestionId());
                reply.setStudentName(userService.findStudentById(findSelectQuestion.get(i).getStudentId()).getName());
                reply.setSelectquestionTitle(questionService.findGiveQuestionById(findSelectQuestion.get(i).getQuestionId()).getTitle());
            }
        }
        reply.setIsEnd(1);
        replyService.saveReply(reply);
        return "redirect:/reply/endreply";
    }
    @RequestMapping("/endreply")
    public String endReply(Model model,Model mode0){
        mode0.addAttribute("UserId",UserId);
        List<SelectQuestion> questions=questionService.findSelectQuestion();
        int a=0;
        for (int i=0;i<questions.size();i++) {
            if (questions.get(i).getStudentId().equals(UserId)){
                BigDecimal questionId = questions.get(i).getQuestionId();
                GiveQuestion question = questionService.selectquestionByquestionId(questionId);
                model.addAttribute("question",question);
            }
        }
        List<Reply> reply=replyService.findReply();
        for(int i=0;i<reply.size();i++) {
            if (questionService.findSelectQuestionById(reply.get(i).getSelectquestionId()).getStudentId().equals(UserId)&&reply.get(i).getIsEnd().equals(1)) {
                a=1;
                break;
            }
        }
        if(a==0){
            return "pages/reply/endApplyReply.html";}
        else{
            return "pages/reply/haveapply.html";
        }
    }
    @RequestMapping("/savereply")
    public String SaveApply(Reply reply,@RequestParam(value = "name", required = false) BigDecimal questionId){
        List<SelectQuestion> findSelectQuestion=questionService.findSelectQuestion();
        for(int i=0;i<findSelectQuestion.size();i++){
            if(findSelectQuestion.get(i).getQuestionId().equals(questionId)){
                reply.setSelectquestionId(findSelectQuestion.get(i).getSelectQuestionId());
                reply.setStudentName(userService.findStudentById(findSelectQuestion.get(i).getStudentId()).getName());
                reply.setSelectquestionTitle(questionService.findGiveQuestionById(findSelectQuestion.get(i).getQuestionId()).getTitle());
            }
        }
        reply.setIsEnd(0);
        replyService.saveReply(reply);
        return "redirect:/reply";
    }
    @RequestMapping
    public String ApplyReply(Model model,Model mode0){
        mode0.addAttribute("UserId",UserId);
        List<SelectQuestion> questions=questionService.findSelectQuestion();
        int a=0;
        for (int i=0;i<questions.size();i++) {
            if (questions.get(i).getStudentId().equals(UserId)){
                BigDecimal questionId = questions.get(i).getQuestionId();
                GiveQuestion question = questionService.selectquestionByquestionId(questionId);
                model.addAttribute("question",question);
            }
        }
        List<Reply> reply=replyService.findReply();
        for(int i=0;i<reply.size();i++) {
            if (questionService.findSelectQuestionById(reply.get(i).getSelectquestionId()).getStudentId().equals(UserId)&&reply.get(i).getIsEnd().equals(0)) {
               a=1;
               break;
            }
        }
        if(a==0){
        return "pages/reply/ApplyReply.html";}
        else{
            return "pages/reply/haveapply.html";
        }
    }
}
